<template>
	<view class="me-page">
		<view style="display: flex;padding: 20px;">
			<view><img style="width: 60px;height: 60px;" :src="userImg" alt=""></view>
			<viwe style="margin-left: 20px;">
				<view style="font-size: 32rpx;font-weight: 500;margin: 4px 0;">{{userInfo.userNm}}</view>
				<view style="font-size: 28rpx;color:#ccc;">ID: {{userInfo._id.substring(0,8)}}</view>
			</viwe>
		</view>
		<view style="margin-top: 20px;background-color: #fff;padding: 20px;">
			<view class="nav-sty" v-for="(item, index) in navList" :key="index" @click="toDetailPage(item.url)">
				<view><img class="nav-icon" :src="item.icon" alt=""></view>
				<view class="nav-title nav-font">{{item.title}}</view>
				<view class="nav-font"> > </view>

			</view>
		</view>
		<view class="out-login-btn" @click="outLogin">退出登入</view>
	</view>
</template>

<script>
	import {
		cheackUser
	} from '@/utils/baseFn.js'
	const db = uniCloud.database();
	const dbCmd = db.command;
	export default {
		data() {
			return {
				userImg: '',
				userInfo: {
					_id: '',
					userNm: ''
				},
				navList: []
			}
		},
		onLoad(e) {},
		onReady() {},
		onShow() {
			const _this = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					if (res.data) {
						const parm = {
							phone: res.data.phone,
							password: res.data.password
						}
						const arr = []
						cheackUser(parm, 'login').then(r => {
							const file = r.data.portrait.split(',')
							_this.userInfo = r.data
							uni.setStorage({
								key: 'userInfo',
								data: r.data
							})
							_this.userImg = `${_this.$baseImgUrl}${file[0]}`
							db.collection('team-MGMT').where(`"createdUserId"=="${r.data._id}"||"_id"=="${r.data.ballTeamNm}"`)
							.field("state,type").get().then((tre) => {
								const url = r.data.userType != 0 ? '../register/playRegister' :'../register/register'
								const data = tre.result.data[0]
								arr.push({
									title: '个人信息',
									url: url,
									icon: '../../../static/geren.png'
								})
								arr.push({
									title: '密码修改',
									url: '../register/editPassword',
									icon: '../../../static/geren.png'
								})
								if (data || r.data.userType == "1" || r.data.userType ==
									"2") { // 球员或球队负责人
									const id = r.data.ballTeamNm?r.data.ballTeamNm:data._id
									const url = data.state != 1 ? `../register/ballTeamRegister?id=${id}&fla=${data.type}&from="me"`:`../ballTeam/ballTeamDetail?id=${id}`
									arr.push({
										title: '我的球队',
										url,
										icon: '../../../static/ballTeam.png'
									}, {
										title: '我的赛事',
										// url: `../competition/competition?id=${r.data.ballTeamNm}`,
										url: `./myEventReg?id=${r.data.ballTeamNm}`,
										icon: '../../../static/ballTeam.png'
									})
									if (r.data.userType == "2") {
										arr.push({
											title: '球队信息修改',
											url: `../ballTeam/ballTeamPhoto?id=${r.data.ballTeamNm}`,
											icon: '../../../static/ballTeam.png'
										})
									}
								}
								if (r.data.classIds) {
									arr.push({
										title: '我的课程',
										url: `./myClass`,
										icon: '../../../static/class.png'
									})
								}
								_this.navList = arr
							}).catch((err) => {
								uni.showModal({
									content: err.message || '请求服务失败',
									showCancel: false
								})
							}).finally(() => {})
						})
					}
				},
				fail: function(res) {
					uni.navigateTo({
						url: '../login/login'
					});
				}
			})
		},
		created() {},
		mounted() {},
		methods: {
			toDetailPage(url) {
				let obj = {}
				if (url == '../register/playRegister') {
					obj = {
						id: this.userInfo.ballTeamNm,
						nm: '',
						typ: this.userInfo.ballTeamType,
						rpbNm: ''
					}
					url = `${url}?parm=${JSON.stringify(obj)}`
				}
				uni.navigateTo({
					url: url
				});
			},
			outLogin() {
				uni.removeStorage({
					key: 'userInfo',
					success: function() {
						uni.showToast({
							title: '登出成功',
							icon: 'none'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '../home/home'
							});
						}, 1228)
					}
				})
			}
		}
	}
</script>

<style>
	.me-page {
		height: 100vh;
		background-color: #f6f7fb;
	}

	.nav-sty {
		display: flex;
		height: 60px;
		line-height: 60px;
		border-bottom: 1px solid #eee;
	}

	.nav-icon {
		width: 30px;
		height: 30px;
		vertical-align: middle;
	}

	.nav-title {
		width: 100%;
		margin-left: 10px;
	}

	.nav-font {
		font-size: 36rpx;
		font-weight: 500;
	}

	.out-login-btn {
		width: 120px;
		height: 36px;
		line-height: 36px;
		text-align: center;
		position: absolute;
		bottom: 20px;
		left: 50%;
		margin-left: -60px;
		background: #ffa52d;
		color: #fff;
	}
</style>
<style lang="scss">
</style>